www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/Verity.cpp
// Verity.cpp: implementation of the CVerity class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Verity.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CVerity::CVerity() { m_asVerityType.Add ("CRC16"); m_asVerityType.Add ("LRC (BCD码,字符之间不许有空格)"); } CVerity::~CVerity() { } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: CRC16------循环容余校验 //---------------------------------------------------- //参数: unsigned char *pucChar-------------接收到的字符 //---------------------------------------------------- //功能: 循环容余校验 //---------------------------------------------------- //返回值: // 校验码 //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ unsigned short CVerity::CRC16(const unsigned char *pucChar, unsigned int unLen) { unsigned short usItem=0xA001; unsigned short usCRCReg=0xFFFF; unsigned char ucChar; for(unsigned int i=0;i<unLen;i++) { ucChar=*(pucChar+i); usCRCReg^=ucChar; for(int j=0;j<8;j++) { bool bLSB=((usCRCReg & 0x0001)== 0x0001); usCRCReg=usCRCReg>>1; if(bLSB) { usCRCReg^=usItem; } } } return usCRCReg; // unsigned char uchCRCHi=0xFF; // unsigned char uchCRCLo=0xFF; // unsigned uIndex; // // while(unLen--) // { // uIndex=uchCRCLo^*pucChar++; //calculate the CRC // uchCRCLo=uchCRCHi^auchCRCHi[uIndex]; // uchCRCHi=auchCRCLo[uIndex]; // } // return(uchCRCHi<<8|uchCRCLo); } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: TransStrHex------将字符串转换成十六进制 //---------------------------------------------------- //参数: //---------------------------------------------------- //功能: 循环容余校验 //---------------------------------------------------- //返回值: // unsigned char* pucHexBuffer 十六进字符串 // int nHexBuf 允许十六进字符串总数 //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ int CVerity::TransStrHex(CString strHex,unsigned char* pucHexBuffer,int nHexBuf) { int nCount=0; int nIndex=0; // pucHex=new unsigned char[200]; // char* pucHex=new char[200]; // char pucHex[200]; CString strHexTmp=strHex; strHexTmp.TrimLeft (); strHexTmp.MakeUpper (); CString strHexByte; unsigned char* pucHexBuf=pucHexBuffer; int nFind=0; while(nFind>=0) { if(nFind=strHexTmp.Find ("0X")==0) //0xAB---去掉0x strHexTmp=strHexTmp.Mid (2); nFind=strHexTmp.Find (" ",0); if(nFind>0) strHexByte=strHexTmp.Mid (0,nFind); else strHexByte=strHexTmp; if(strHexByte.GetLength ()==0) break; strHexByte.TrimLeft (); strHexTmp=strHexTmp.Mid (nFind); strHexTmp.TrimLeft (); unsigned char ucByteH; unsigned char ucByteL; if(strHexByte.GetLength ()<2) { ucByteH=0x00; char cByte=strHexByte.GetAt (0); if(cByte>='A') ucByteL=cByte-'A'+10; else ucByteL=cByte-'0'; } else { // char cByte=strHexByte.GetAt (0); if(cByte>='A') ucByteH=cByte-'A'+10; else ucByteH=cByte-'0'; cByte=strHexByte.GetAt (1); if(cByte>='A') ucByteL=cByte-'A'+10; else ucByteL=cByte-'0'; } unsigned char ucByte=0; ucByte|=ucByteH<<4; ucByte|=ucByteL; if(nHexBuf-1<nIndex) return -1; pucHexBuf[nIndex++]=ucByte; } return nIndex; } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: LRC------纵向异或校验 //---------------------------------------------------- //参数: const CString& strTxt-------------字符串 //---------------------------------------------------- //功能:纵向异或校验 //---------------------------------------------------- //返回值: // 校验码(2个 char) //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ unsigned char CVerity::LRC(CString strTxt) { if(strTxt.IsEmpty()) return 0; strTxt.MakeUpper (); unsigned char ucLRC=0; int nCount=strTxt.GetLength(); if(nCount%2!=0) { CString strTip; strTip.Format("LRC校验:字符数量只有%d,字符数量应该为偶数",nCount); AfxMessageBox(strTip); return 0; } BYTE byLRC = 0; char pBuf[4]; pBuf [2] = '\0'; int nData = 0; for(int i=0; i<nCount; i+=2) { //每两个需要发送的ASCII码转化为一个十六进制数 pBuf [0] = strTxt.GetAt (i); pBuf [1] = strTxt.GetAt (i+1); // pBuf [2] = '\0'; sscanf(pBuf,"%x",& nData); byLRC += nData; } byLRC = ~ byLRC; byLRC ++; return byLRC; } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: SumHex------Hex和校验 //---------------------------------------------------- //参数: unsigned char *pucChar-------------接收到的字符 //---------------------------------------------------- //功能:和校验 //---------------------------------------------------- //返回值: // UINT //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ UINT CVerity::SumHex(const unsigned char *pucChar, unsigned int unLen) //计算和 --SumAsc(0x31 0x32 ) = 0x63 { UINT unSum = 0; unsigned char ucChar; for(unsigned int i = 0;i< unLen; i++) { ucChar = *(pucChar+i); unSum += ucChar; } return unSum; } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: SumHex------AscNum和校验 //---------------------------------------------------- //参数: unsigned char *pucChar-------------接收到的字符 //---------------------------------------------------- //功能:AscNum和校验 //---------------------------------------------------- //返回值: // UINT //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ UINT CVerity::SumAscNum(const unsigned char *pucChar, unsigned int unLen) //计算和 --SumAsc(0x31 0x32 ) = 0x03 { UINT unSum = 0; unsigned char ucChar; for(unsigned int i = 0;i< unLen; i++) { ucChar = *(pucChar+i); unSum += ucChar - 0x30; } return unSum; }